package com.jzo2o.customer.controller.consumer;


import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 用户地址相关接口")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 查询用户默认地址
     * @return
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("查询用户默认地址值")
    public AddressBookResDTO findDefaultAddress(){
        return addressBookService.findDefaultAddress();
    }


    /**
     * 新增用户地址
     * @param addressBookUpsertReqDTO
     */
    @ApiOperation("新增用户地址")
    @PostMapping()
    public void add(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO){
        addressBookService.add(addressBookUpsertReqDTO);
    }

    /**
     * 地址薄分页查询
     * @param addressBookPageQueryReqDTO
     * @return
     */
    @ApiOperation("地址薄分页查询")
    @GetMapping("/page")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookPageQueryReqDTO) {
        return addressBookService.page(addressBookPageQueryReqDTO);
    }

    /**
     * 查询地址簿详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询地址簿详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class)
    })
    public AddressBookResDTO getAddressInfo (@PathVariable("id") Long id){
        return addressBookService.getAddressInfo(id);
    }

    /**
     * 更新地址簿
     * @param id
     * @param addressBookUpsertReqDTO
     */
    @PutMapping("/{id}")
    @ApiOperation("更新地址簿")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class)
    })
    public void update(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO){
        addressBookService.update(id, addressBookUpsertReqDTO);
    }

    /**
     * 批量删除地址簿
     * @param ids
     */
    @DeleteMapping("batch")
    @ApiOperation("批量删除地址簿")
    public void delete(@RequestBody List<Long> ids){
        addressBookService.removeByIds(ids);
    }

    /**
     * 设置默认地址
     * @param flag
     * @param id
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "是否默认", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "id", value = "地址簿id", required = true, dataTypeClass = Long.class)
    })
    public void defaultAddress(Long flag,Long id){
        addressBookService.defaultAddress(flag,id);
    }

}
